// Copyright 2023 Srhino Co., Ltd. All rights reserved.

package discovery

import "errors"

type RequestMethod int32

const (
	RequestMethod_METHOD_UNKNOWN RequestMethod = iota
	RequestMethod_GET
	RequestMethod_HEAD
	RequestMethod_POST
	RequestMethod_PUT
	RequestMethod_DELETE
	RequestMethod_CONNECT
	RequestMethod_OPTIONS
	RequestMethod_TRACE
	RequestMethod_PATCH
)

var (
	NotMatched = errors.New("not matched api")

	DefaultRules = []*DiscoverRule{
		{
			RequestHeader: RequestHeader{
				ContentType: []string{"application/(x-www-form-urlencoded|form-data|(.*json.*)|(.*xml.*))"},
				Method:      []string{},
				Accept:      []string{},
				URL:         []string{"((.*/api/.*)|(.*/(v|V)\\d{1,}/.*)|(.*/\\d{1,}.\\d{1,}/.*))"},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{"application/(x-www-form-urlencoded|form-data|(.*json.*)|(.*xml.*))"},
				Method:      []string{},
				Accept:      []string{},
				URL:         []string{},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{"application/(x-www-form-urlencoded|form-data|(.*json.*)|(.*xml.*))"},
				Method:      []string{},
				Accept:      []string{},
				URL:         []string{},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{},
				Method:      []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
				Accept:      []string{},
				URL:         []string{"((.*/api/.*)|(.*/(v|V)\\d{1,}/.*)|(.*/\\d{1,}.\\d{1,}/.*))"},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{},
				Method:      []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
				Accept:      []string{"application/((.*json.*)|(.*xml.*))"},
				URL:         []string{},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{},
				Method:      []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
				Accept:      []string{},
				URL:         []string{},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{},
				Method:      []string{},
				Accept:      []string{},
				URL:         []string{"((.*/api/.*)|(.*/(v|V)\\d{1,}/.*)|(.*/\\d{1,}.\\d{1,}/.*))"},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{},
			},
		},
		{
			RequestHeader: RequestHeader{
				ContentType: []string{},
				Method:      []string{},
				Accept:      []string{},
				URL:         []string{},
			},
			ResponseHeader: ResponseHeader{
				ContentType: []string{"application/((.*json.*)|(.*xml.*))"},
			},
		},
	}
)

type ApiRequest struct {
	Method          string
	URL             string
	ResponseCode    uint32
	RequestHeaders  ApiRequestHeaders
	ResponseHeaders ApiResponseHeaders
}

type ApiRequestHeaders struct {
	Accept      string
	ContentType string
}

type ApiResponseHeaders struct {
	ContentType string
}

type DiscoverRule struct {
	//请求头规则
	RequestHeader RequestHeader `json:"request,omitempty" bson:"request"`
	//响应头规则
	ResponseHeader ResponseHeader `json:"response,omitempty" bson:"response"`
	//权重
	Weight int8 `json:"weight,omitempty" bson:"weight"`
	//内置过滤器
	IsInternal bool `json:"is_internal,omitempty" bson:"is_internal"`
	//是否禁用
	Disabled bool `json:"disabled,omitempty" bson:"disabled"`
}

type RequestHeader struct {
	ContentType []string `json:"content_type,omitempty" bson:"content_type"`
	Method      []string `json:"method,omitempty" bson:"method"`
	Accept      []string `json:"accept,omitempty" bson:"accept"`
	URL         []string `json:"url,omitempty" bson:"url"`
}

type ResponseHeader struct {
	ContentType        []string `json:"content_type,omitempty" bson:"content_type"`
	URL                []string `json:"url,omitempty" bson:"url"`
	IgnoreResponseCode []uint32 `json:"response_code,omitempty" bson:"response_code"`
}
